<template>
  <div class="init-container">
    <div class="mask-container" v-if="isMask"></div>
    <div class="none-container" v-if="nonePermission">
      {{ $srv.getI18nByPath('others.noPermissions') }}···
    </div>
    <vxe-grid
      ref="xGrid"
      v-bind="G_gridOptions"
      height="auto"
      @cell-dblclick="onCellDblclick"
      @cell-click="onCellClick"
    >
    </vxe-grid>
  </div>
</template>

<script lang="ts">
import { I_cmd, T_srv, Tcmd, Tsrv, getGridOpt, ifs, ts, vue } from './config';
import { N_oBaseGridPlus } from './types';

@T_srv.comp
export default class oBaseGridPLus extends vue {
  /* 父组件compBoxInfo */
  @T_srv.prop({
    required: false,
    type: Object,
    default: null
  })
  compBoxInfo: ifs.I_compBoxInfo;

  /**
 {
  tableName:'xxx', //mongoDB 表名
  autoLoad:true,
  cloumns:[] //vxeTable Cloumns
 }
 */
  @T_srv.prop({
    required: true,
    type: Object,
    validator: v => !!v?.tableName
  })
  config: N_oBaseGridPlus.I_conf;

  @T_srv.ref('xGrid') G_grid: any;

  /* 组件名 */
  private G_compName: string = 'oBaseGridPLus';

  /* 组件箱信息对象 */
  private GT_compBoxInfo: ifs.I_compBoxInfo;

  /* 组件箱对象 */
  private GT_compBox: ifs.I_compBox;

  /* 组件箱识别ID */
  private G_compID: string;

  public G_gridOptions: N_oBaseGridPlus.I_tableOption = undefined;

  private G_data: Array<any> = [];

  /* 指挥部 */
  private GT_command: I_cmd = Tcmd;

  public isMask: boolean = false;

  private nonePermission: boolean = false;

  public funName: string = '';

  created() {
    // 如果父组件没有传参，那就产生一个
    this.GT_compBoxInfo = Tsrv.getCompInfo(this.compBoxInfo);
    this.G_compID = this.GT_compBoxInfo.compID;
    this.GT_compBox = this.GT_compBoxInfo.TcompBox;
    this.GT_command.init(this, this.GT_compBoxInfo);
    this.funName = this.config?.tableName || '';

    this.G_gridOptions = getGridOpt(this);
    this.G_gridOptions.autoLoad = this.config?.autoLoad ?? true;
    this.G_gridOptions.columns = this.config?.columns ?? [];

    /* 如果外部有传入组件信息 */
    if (Tsrv.utils.isEmpty(this.compBoxInfo)) {
      this.GT_compBox = new ts.T_compBox(this.G_compID); // 使用本地实例
    } else {
      this.G_compID = this.compBoxInfo.compID;
      this.GT_compBox = this.compBoxInfo.TcompBox;
    }
    this.GT_compBoxInfo = { compID: this.G_compID, TcompBox: this.GT_compBox };

    // 功能“浏览”权限
    // const defaultPermission = Tsrv.utils.find(this.config.permission, item =>
    //   item.area === 'default'
    // )
    // this.nonePermission = !defaultPermission?.data?.browse;
  }

  loadData() {
    this.GT_command.loadData();
  }

  getData(): Array<any> {
    return this.G_data;
  }

  mounted() {
    /* 向父组件发送实例 */
    // this.$emit('compBoxInfo', this.GT_compBoxInfo);
    this.sendCompInfo(this.GT_compBoxInfo);
    this.GT_compBox.addCompMergeName(this.G_compID, this.G_compName, this);
    /* 最终应用组件向外抛出引用实例,以供应用间调用 */
    Tsrv.compBox.addCompMergeName(
      Tsrv.compBox.getDefGUID(), // 全局compBox
      this.funName,
      this
    );
    this.config?.autoLoad && this.loadData();
  }

  destroyed() {
    /* 注册组件销毁 */
    this.GT_compBox.removeCompMergeName(this.G_compID, 'this.G_compName');
    /* 最终应用组件销毁 */
    Tsrv.compBox.removeCompMergeName(Tsrv.compBox.getDefGUID(), this.funName);

    /* 如果父组件传入的compBoxInfo则删除本文件产生的引用,反之是本组件产生的ID统一删除 */
    if (Tsrv.utils.isEmpty(this.compBoxInfo)) {
      this.GT_compBox.removeCompsWithCompID(this.G_compID);
    } else {
      this.GT_compBox.removeCompsMergeName(this.G_compID, [
        `'${this.G_compName}'`
      ]);
    }
    this.GT_compBox = null;
  }

  @T_srv.emit('compBoxInfo')
  sendCompInfo(compBoxInfo: ifs.I_compBoxInfo) {}

  @T_srv.emit('on-cell-click')
  onCellClick(e) {}

  @T_srv.emit('on-cell-dblclick')
  onCellDblclick(e) {}
}
</script>
<style lang="scss" scoped>
::v-deep .ivu-drawer-body {
  padding: 0px;
}
::v-deep .vxe-toolbar {
  border-top: 2px solid #f4f4f4;
  padding: 6px;
}
::v-deep .vxe-button {
  border: 1px solid #fff;
}
</style>
